package com.cloud.jdkTest;

import java.util.stream.IntStream;

public class ParallelStreamTest {

    public static void main(String[] args) {
        long t0 = System.nanoTime();

        //初试化一个范围100万的整数流，求能被2整除的数字，toArray()是终点方法
        int a[] = IntStream.range(0, 1_000_000).filter( p -> p % 2 == 0).toArray();

        long t1 = System.nanoTime();

        //使用并行流来计算
        int b[] = IntStream.range(0, 1_000_000).parallel().filter( p -> p % 2 == 0).toArray();

        long t2 = System.nanoTime();

        System.out.printf("serial: %.2fs, parallel %.2fs%n", (t1 - t0) * 1e-9, (t2 - t1) * 1e-9);
    }
}
